<?php

class Model_Dishes
{
	protected $_table;
	
	public function getTable()
	{
		if(null === $this_table)
		{
			require_once(APPLICATION_PATH.'/models/dbtable/fooditems.php');
			$this->_table = new fooditems();
		}
		return $this->_table;
	}
	
	public function save(array $data)
    {
        $table  = $this->getTable();
        $fields = $table->info(Zend_Db_Table_Abstract::COLS);
        foreach ($data as $field => $value) {
            if (!in_array($field, $fields)) {
                unset($data[$field]);
            }
        }
        return $table->insert($data);
    }

    /**
     * Fetch all entries
     * 
     * @return Zend_Db_Table_Rowset_Abstract
     */
    public function fetchEntries()
    {
        // we are gonna return just an array of the data since
        // we are abstracting the datasource from the application,
        // at current, only our model will be aware of how to manipulate
        // the data source (dbTable).
        // This ALSO means that if you pass this model
        //return $this->getTable()->fetchAll('1')->toArray();
		$db = Zend_Registry::get('db');
    	$sql = "SELECT * FROM categories a,fooditems b where a.cat_id = b.cat_id_item order by a.cat_name";
        $result = $db->fetchAll($sql);
        //$db->fetchAll($sql);
        
        return $result;
    }
	public function delete($id)
	{	
	
		$table = $this->getTable();
		$delete = $table->delete('item_id='.$id);
		
	}
    /**
     * Fetch an individual entry
     * 
     * @param  int|string $id 
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntry($id)
    {
        $table = $this->getTable();
        $select = $table->select()->where('item_id = ?', $id);
        // see reasoning in fetchEntries() as to why we return only an array
        return $table->fetchRow($select)->toArray();
    }

}
